<template>
  <div class="app-container">
    <!--1.查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="hospitalSetQueryVo.hosname" placeholder="医院名称" />
      </el-form-item>

      <el-form-item>
        <el-input v-model="hospitalSetQueryVo.hoscode" placeholder="医院编号" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
      <!-- 1.2工具条 -->
      <el-button type="danger" @click="removeRows()">批量删除</el-button>
    </el-form>

    <!-- 2.表格渲染 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      style="width: 100%"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <!-- 2.1每行记录前添加一个复选框 -->
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="hosname" label="医院名称" width="180" />

      <el-table-column prop="hoscode" label="医院编号" width="160" />

      <el-table-column prop="apiUrl" label="地址" width="200" />

      <el-table-column prop="contactsName" label="联系人" />

      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="280" align="center">

        <template slot-scope="scope">

          <el-button v-if="scope.row.status==1" type="info" size="mini"
            icon="el-icon-lock" @click="lockHostSet(scope.row.id,0)">锁定</el-button>
          <el-button v-if="scope.row.status==0" type="warning" size="mini"
            icon="el-icon-unlock" @click="lockHostSet(scope.row.id,1)">解锁</el-button>
          &nbsp;
          <!-- 隐藏路由 -->
          <router-link :to="'/hospset/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button >
          </router-link>
          &nbsp;
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 3.分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import hospset from "@/api/hospital/hospset";

export default {
  // 定义变量和初始值
  data() {
    return {
      page: 1, // 当前页
      limit: 10, // 每页记录数
      list: null, // 每页数据list集合
      total: 0, // 总记录数
      hospitalSetQueryVo: {}, // 查询条件
      listLoading: false, // 是否显示loading信息
      multipleSelection: [], // 批量选择中选择的记录列表
    };
  },
  // 渲染之前执行,调用方法
  created() {
    this.fetchData();
  },
  methods: {
    /**
     * 锁定和取消锁定
     */
    lockHostSet(id,status) {
      hospset.lockHospSet(id,status)
          .then(response => {
            //刷新
            this.fetchData()
          })
    },
    /**
     * 5.批量删除
     */
    removeRows(id) {
      this.$confirm("此操作将批量永久删除医院信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        var idList = [];
        //遍历数组得到每个id值，设置到idList里面
        for (var i = 0; i < this.multipleSelection.length; i++) {
          // 从this.multipleSelection数组获取所有id值
          var obj = this.multipleSelection[i];
          var id = obj.id;
          idList.push(id);
        }
        hospset.batchRemoveHospitalSet(idList).then((response) => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },

    /**
     * 4.当表格复选框选项发生变化的时候触发
     */
    handleSelectionChange(selection) {
      // selection会获取复选框所在行的内容
      this.multipleSelection = selection;
    },
    /**
     * 3.根据id删除
     */
    removeDataById(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return hospset.removeById(id);
        })
        .then(() => {
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
    },
    /**
     * 2.清空功能
     */
    resetData() {
      // 清空表单
      this.hospitalSetQueryVo = {};
      // 查询所有数据
      this.fetchData();
    },
    /**
     * 1.分页查询
     */
    fetchData(current = 1) {
      this.page = current;
      hospset
        .getPageList(this.page, this.limit, this.hospitalSetQueryVo)
        .then((response) => {
          if (response.success === true) {
            this.total = response.data.total;
            this.list = response.data.rows;
          }
        });
    },
  },
};
</script>
